{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q starrocks \"sqlalchemy<3.0\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This CATALOG_URL works for the \"docker compose\" testing and development environment\n",
    "# Change 'lakekeeper' if you are not running on \"docker compose\" (f. ex. 'localhost' if Lakekeeper is running locally).\n",
    "CATALOG_URL = \"http://lakekeeper:8181/catalog\"\n",
    "STARROCKS_URI = \"starrocks://root@starrocks:9030\"\n",
    "WAREHOUSE = \"demo\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Starrocks Catalog"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine, text\n",
    "\n",
    "engine = create_engine(STARROCKS_URI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Until https://github.com/StarRocks/starrocks/issues/50585 is fixed, we have to configure\n",
    "# the credentials locally if we need a custom s3 endpoint.\n",
    "with engine.connect() as connection:\n",
    "    connection.execute(text(\"DROP CATALOG IF EXISTS lakekeeper\"))\n",
    "    connection.execute(\n",
    "        text(f\"\"\"\n",
    "        CREATE EXTERNAL CATALOG lakekeeper\n",
    "        PROPERTIES\n",
    "        (\n",
    "            \"type\" = \"iceberg\",\n",
    "            \"iceberg.catalog.type\" = \"rest\",\n",
    "            \"iceberg.catalog.uri\" = \"{CATALOG_URL}\",\n",
    "            \"iceberg.catalog.warehouse\" = \"{WAREHOUSE}\",\n",
    "            \"aws.s3.region\" = \"local\",\n",
    "            \"aws.s3.enable_path_style_access\" = \"true\",\n",
    "            \"aws.s3.endpoint\" = \"http://minio:9000\",\n",
    "            \"aws.s3.access_key\" = \"minio-root-user\",\n",
    "            \"aws.s3.secret_key\" = \"minio-root-password\"\n",
    "        )\n",
    "        \"\"\")\n",
    "    )\n",
    "    connection.execute(text(\"SET CATALOG lakekeeper\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read and Write Tables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with engine.connect() as connection:\n",
    "    connection.execute(text(\"CREATE DATABASE starrocks_namespace\"))\n",
    "    connection.execute(\n",
    "        text(\"CREATE TABLE starrocks_namespace.my_table (my_ints INT, my_floats DOUBLE, strings VARCHAR)\")\n",
    "    )\n",
    "    connection.execute(\n",
    "        text(\"INSERT INTO starrocks_namespace.my_table VALUES (1, 1.0, 'a'), (2, 2.0, 'b')\")\n",
    "    )\n",
    "    result = connection.execute(\n",
    "        text(\"SELECT * FROM starrocks_namespace.my_table\"),\n",
    "    ).fetchall()\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
